﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Giohang : System.Web.UI.Page
{
    DADataContext da = new DADataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
                Display();
        }

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ShoppingCart Cart = (ShoppingCart)Session["GioHang"];
        Cart.Delete(GridView1.DataKeys[e.RowIndex].Value.ToString());
        Display();
    }
    void Display()
    {
        ShoppingCart cart = (ShoppingCart)Session["GioHang"];
        GridView1.DataSource = cart;
        GridView1.DataBind();
        Sum.Text = String.Format("{0:0,0 VNĐ}", cart.Quantity);

        pnTongTien.Visible = cart.Count > 0;
        pnDatHang.Visible = cart.Count > 0;
    }
    protected void Update_Click(object sender, EventArgs e)
    {
        ShoppingCart Cart = (ShoppingCart)Session["GioHang"];
        foreach (GridViewRow GVR in GridView1.Rows)
        {
            TextBox tb = (TextBox)GVR.Cells[3].FindControl("TextBox1");
            LinkButton lb = (LinkButton)GVR.Cells[1].FindControl("LinkButton1");
            if (int.Parse(tb.Text) <= 0)
            {
                Cart.Delete(lb.CommandArgument.ToString());
            }
            
            Cart.Update(lb.CommandArgument.ToString(), double.Parse(tb.Text));
        }
        Display();
    }
    protected void Tiep_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Online/Default.aspx");
    }
    protected void btnDatHang_Click(object sender, EventArgs e)
    {
        DatHang d = new DatHang();
        Guid IDDH = Guid.NewGuid();
        d.ID = IDDH;
        d.HoTenKH = txtHoTen.Text.Trim();
        d.DiaChi = txtDiaChi.Text.Trim();
        d.Email = txtEmail.Text.Trim();
        d.NoiDung = txtYeuCau.Text.Trim();
        d.SoDienThoai = txtSoDienThoai.Text.Trim();
        d.NgayDatHang = DateTime.Now;
        da.DatHangs.InsertOnSubmit(d);
        da.SubmitChanges();

        foreach (GridViewRow GVR in GridView1.Rows)
        {
            TextBox txt = (TextBox)GVR.Cells[3].FindControl("TextBox1");
            Label lb = (Label)GVR.Cells[4].FindControl("lbID");

            ChiTietDH ct = new ChiTietDH();
            ct.ID = Guid.NewGuid();
            ct.IDDatHang = IDDH;
            ct.IDSanPham = int.Parse(lb.Text.Trim());
            ct.SoLuong = int.Parse(txt.Text.Trim());

            da.ChiTietDHs.InsertOnSubmit(ct);   
        }
        da.SubmitChanges();
        txtHoTen.Text = "";
        txtDiaChi.Text = "";
        txtEmail.Text = "";
        txtSoDienThoai.Text = "";
        txtYeuCau.Text = "";

        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('Đã gửi đơn hàng thành công, chúng tôi sẽ liên hệ với bạn trong thời gian sớm nhất')", true);

    }
}
